home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************
- *
- * memFree.c
- *
- * external module for Apple Help
- *
- * Author: Josh Jacobs
- * Copyright: 1993 Apple Computer, Inc
- * Date: 5/3/93
- *
- */
-
- #include "memFree.h"
-
-
-
- /*******************************************************
- *
- * Function prototypes
- *
- */
-
- unsigned long GetFreeSystemMemory(unsigned long needed);
- OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize);
-
-
- /*******************************************************
- *
- * main()
- *
- */
-
- pascal OSErr
- main(memFreeDataPtr msg, Size inSize, void* outMessage, Size* outSize, Handle ignoreMe)
- {
- Boolean result = false;
- OSErr myErr = noErr;
- unsigned long curFreeMem,needed;
-
- needed = msg->amountNeeded * 1000;
-
- curFreeMem = GetFreeSystemMemory(needed);
-
- result = needed <= curFreeMem;
-
- myErr = SetContextResult(&result, sizeof(Boolean), outMessage, outSize);
- return(myErr);
- }
-
- /*******************************************************
- *
- * GetFreeSystemMemory()
- *
- * call tempFreeMem to see how much memory is available.
- * it calls compactMem only if the amount is not enough.
- * this is to ensure that we are not thrashing the system
- * heap by repeated compaction as Apple Help continues to evaluate
- * our context.
- *
- */
-
- unsigned long GetFreeSystemMemory(unsigned long needed)
- {
- unsigned long freeMem;
- THz oldZone = GetZone();
-
- SetZone(SystemZone());
- freeMem = TempFreeMem();
-
- if (freeMem < needed) // Only compact if we need more memory
- freeMem = CompactMem(needed);
-
- SetZone(oldZone);
-
- return(freeMem);
- }
-
-
- /*******************************************************
- *
- * SetContextResult()
- *
- * set the result up for Apple Help.
- */
-
- OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
- {
- Ptr p;
-
- if (p = NewPtr(theSize))
- {
- BlockMove(theData, p, theSize);
-
- *outSize = theSize;
- *outMessage = p;
-
- return(noErr);
- }
- else
- return(MemError());
- }
-